package com.kyzh.core.activities

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setHeight
import com.gushenge.atools.util.setMargins
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.R
import com.kyzh.core.adapters.decoration.SpacesItemDecoration
import com.kyzh.core.current.AnswerBean
import com.kyzh.core.current.AnswerOption
import com.kyzh.core.current.WealImpl1.WealImpl1
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_answer.*
import kotlinx.android.synthetic.main.activity_product.view.*
import org.jetbrains.anko.backgroundResource


class AnswerActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, AnswerActivity::class.java)
            context.startActivity(starter)
        }
    }
    var id : String = ""
    val options = mutableListOf<AnswerOption>()
    var payType :String? = null
    lateinit var optionadapter :OptionAdapter
    lateinit var topadapter:TopAdapter
    var topBean = ArrayList<AnswerBean.Rili>()
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_answer)
        close.setOnClickListener {
            finish()
        }
        btnSummit.isEnabled =  false
        initView()

        initdata()
    }
    fun  initView(){
        top.setHeight(AView.getStatusBarHeight(this)+dip(230))
        close.setMargins(0,AView.getStatusBarHeight(this),0,0)
        rcvData.layoutManager =LinearLayoutManager(this@AnswerActivity, RecyclerView.HORIZONTAL, false)
        revOption.layoutManager =LinearLayoutManager(this@AnswerActivity, RecyclerView.VERTICAL, false)
        revOption.addItemDecoration(SpacesItemDecoration(8f))
    }
    fun  initdata(){
        WealImpl1.answer {
            val  answerBean = this
            payType = answerBean?.my?.type
            if(this== null){
                finish()
            }else {
                tvContextTitle.text = "今日趣味问答（+${this.my.number})"
                if(this.my.total=="0"){
                    tvContextTip.text = "恭喜您，本月已累计获得奖励${this.my.total}${this.my.type}，快去答题吧～"
                }else{
                    tvContextTip.text = "恭喜您，本月已累计获得奖励${this.my.total}${this.my.type}，快去使用吧～"
                }
                tvIssue.text = answer.name
                when(my.today_answer){
                     0 ->
                         btnSummit.text = "提交"
                    1 ->
                        btnSummit.text = "恭喜你，回答正确"
                    2 ->
                        btnSummit.text = "别灰心，明天再来试试吧"
                }
            }
            answerBean?.rili?.let{ it ->
                topBean = it
                topadapter = TopAdapter(topBean)
                rcvData.adapter =topadapter
                it.size.let {
                    rcvData.scrollToPosition(it-1)
                }
            }
            answerBean?.answer?.let {
                id = it.id
                val success = it.`true`
                val submit = it.submit
                options.clear()
                options.add(AnswerOption(0,it.optiona,"optiona"))
                options.add(AnswerOption(0,it.optionb,"optionb"))
                options.add(AnswerOption(0,it.optionc,"optionc"))
                options.add(AnswerOption(0,it.optiond,"optiond"))
                optionadapter =OptionAdapter()
                revOption.adapter =optionadapter
                if(submit.trim().isNotEmpty()){
                    if(success == submit){
                        imgTag.setImageResource(R.drawable.icon_success)
                    }else{
                        imgTag.setImageResource(R.drawable.icon_error)
                    }
                    options.forEach {
                        if(success == submit){
                            if(success == it.subOption){
                                it.checkState=2
                            }
                        }else{
                            if(success == it.subOption){
                                it.checkState=2
                            }
                            if(submit == it.subOption){
                                it.checkState=3
                            }
                        }
                    }
                }else{
                    imgTag.visibility = View.GONE
                }
                if(it.submit.trim().isNullOrEmpty()){
                    btnSummit.backgroundResource =  R.drawable.bg_answer_submit
                    optionadapter.setOnItemClickListener { a, view, position ->
                        //选中后其他的只灰
                        options.forEach {bean->
                            bean.checkState=0
                        }
                        options[position].checkState = 1
                        optionadapter.setNewInstance(options)
                        optionadapter.notifyDataSetChanged()
                        //只有选中了才会变红色
                        btnSummit.backgroundResource =  R.drawable.bg_answer_submit2
                        btnSummit.isEnabled =  true
                    }
                }else{
                    btnSummit.backgroundResource =  R.drawable.bg_answer_submit
                    optionadapter.setOnItemClickListener { a, view, position ->
                        return@setOnItemClickListener
                    }
                }
            }
            btnSummit.setOnClickListener {
                //提交逻辑
                submit()
            }
        }
    }
    fun  submit (){
        options.forEach {
            if (it.checkState == 1){
                WealImpl1.answerSubmit(it.subOption,id){
                    val subOption = this
                    optionadapter.setOnItemClickListener { adapter, view, position ->
                        return@setOnItemClickListener
                    }
                    val lastIndex = topBean.lastIndex
                    //刷新顶部显示ui

                    topBean[lastIndex] = topBean.last().apply {
                        answer_status = subOption?.status!!
                        showTopTag=true
                        answer_title = subOption?.jiangli
                    }

                    topadapter.notifyItemChanged(lastIndex)
                    //刷新中部显示ui
                    if(this?.total=="0"){
                        tvContextTip.text = "恭喜您，本月已累计获得奖励${this?.total}${payType}，快去答题吧～"
                    }else{
                        tvContextTip.text = "恭喜您，本月已累计获得奖励${this?.total}${payType}，快去使用吧～"
                    }
                    btnSummit.backgroundResource = R.drawable.bg_answer_submit
                    btnSummit.isEnabled =  false
                    imgTag.setVisibility(true)
                    if(this?.status == 1){
                        options.forEach {
                            if(it.subOption ==this?.submit){
                                it.checkState = 2
                            }
                        }
                        btnSummit.text = "恭喜你，回答正确"
                        imgTag.setImageResource(R.drawable.icon_success)
                    }else{
                        options.forEach {
                            if(it.subOption ==this?.submit){
                                it.checkState = 3
                            }
                            if(it.subOption ==this?.`true`){
                                it.checkState = 2
                            }
                        }
                        btnSummit.text = "别灰心，明天再来试试吧"
                        imgTag.setImageResource(R.drawable.icon_error)
                    }
                    optionadapter.notifyDataSetChanged()
                }
            }
        }
    }

    //上部时间区
    inner class TopAdapter( var rili :ArrayList<AnswerBean.Rili>) : BaseQuickAdapter<AnswerBean.Rili, BaseViewHolder>(R.layout
        .item_answer_top, rili) {
        override fun convert(holder: BaseViewHolder, item: AnswerBean.Rili) {
            holder.apply {
                setText(R.id.tag, "${item.answer_title}")
                when (item.answer_status) {

                    2 -> {
                        setBackgroundResource(R.id.tag, R.drawable.item_answer_white)
                        setTextColorRes(R.id.tag,R.color.font_99)
                    }
                    1 -> {
                        setText(R.id.tag, "+${item.answer_title}")
                        setBackgroundResource(R.id.tag, R.drawable.item_answer_green)
                        setTextColorRes(R.id.tag,R.color.white)
                    }
                    else -> {
                        setBackgroundResource(R.id.tag, R.drawable.item_answer_white)
                        setTextColorRes(R.id.tag,R.color.font_99)
                    }
                }
                when (getItemPosition(item)){
                    0 ->{
                        setVisible(R.id.viewLeft,false)
                        setVisible(R.id.viewRight,true)
                    }
                    rili.size-1 ->{
                        setVisible(R.id.viewLeft,true)
                        setVisible(R.id.viewRight,false)
                    }
                    else ->{
                        setVisible(R.id.viewLeft,true)
                        setVisible(R.id.viewRight,true)
                    }
                }
                if(getItemPosition(item)== rili.lastIndex)
                    item.showTopTag = true
                if(item.showTopTag){
                    setVisible(R.id.viewLeft,true)
                    setVisible(R.id.viewRight,false)
                }
                setVisible(R.id.iv_today, item.showTopTag)
                setText(R.id.data, item.time)
            }
        }
    }
    //问答区
    inner  class  OptionAdapter :  BaseQuickAdapter<AnswerOption, BaseViewHolder>(R.layout.item_answer_option, options) {
        override fun convert(holder: BaseViewHolder, item: AnswerOption) {
            holder.apply {
                setText(R.id.tvOption, item.option)
                val rbOption = getView<RadioButton>(R.id.rbOption)
                when(item.checkState){
                    //默认的
                    0 -> {
                        setVisible(R.id.tvTips, false)
                        setVisible(R.id.imgpanduan, false)
                        setBackgroundResource(R.id.rbOption, R.drawable.icon_answer_uncheck)
                        setBackgroundResource(R.id.item_option,R.drawable.act_search_edittext_bg)
                    }
                    //选中效果
                    1 -> {
                        rbOption.isChecked=true
                        rbOption.setBackgroundResource(R.drawable.icon_answer_check)
                        options[getItemPosition(item)].checkState = 1
                        setVisible(R.id.tvTips, false)
                        setVisible(R.id.imgpanduan, false)
                        setBackgroundResource(R.id.item_option,R.drawable.act_search_edittext_bg_1)
                    }
                    //正确效果
                    2 ->{
                        rbOption.isChecked=true
                        options[getItemPosition(item)].checkState = 2
                        setText(R.id.tvTips,"正确答案")
                        setTextColorRes(R.id.tvTips,R.color.green)
                        setImageResource(R.id.imgpanduan, R.drawable.icon_answer_success)
                        setVisible(R.id.tvTips, true)
                        setVisible(R.id.imgpanduan, true)
                        rbOption.setBackgroundResource(R.drawable.icon_check_success)
                        setBackgroundResource(R.id.item_option,R.drawable.act_search_edittext_bg_green)
                    }
                    //错误效果
                    3 ->{
                        rbOption.isChecked=true
                        options[getItemPosition(item)].checkState = 3
                        setText(R.id.tvTips,"回答错误")
                        setTextColorRes(R.id.tvTips,R.color.red1)
                        setImageResource(R.id.imgpanduan, R.drawable.icon_answer_error)
                        setVisible(R.id.tvTips, true)
                        setVisible(R.id.imgpanduan, true)
                        rbOption.setBackgroundResource(R.drawable.icon_check_error)
                        setBackgroundResource(R.id.item_option,R.drawable.act_search_edittext_bg_red)
                    }
                }
                setOnItemChildClick(this.itemView, getItemPosition(item))
            }
        }
    }
}


